#include "window_main.h"	
#include "window_manager.h"
#include "window_watch_face.h"
Watch_Face_t  Current_Face = Watch_Face_3;//Watch_Face_1;  //当前全局的表盘指定

extern window_manger_t window_manger[];
extern lv_obj_t* current_time;  //显示当前时间
//全局的时间结构体
Time_t My_time=
{
	.year=2022,
	.month=1,
	.day=7,
	.hour=12,
	.minute=52,
	.second=37,
}; 


//电量数值
uint8_t    battrey_value=99;
lv_obj_t * battrey_img=NULL;   //电量对象


//当前时间对象
static   lv_obj_t * time_obj=NULL;




static  lv_obj_t * hour_img=NULL;
static	lv_obj_t * minute_img=NULL;
static   lv_obj_t * second_img=NULL;


static lv_obj_t * task_obj=NULL; //快捷任务栏

static lv_obj_t *  menu_obj=NULL;


static lv_timer_t * timer = NULL;  //设置定时器

static lv_timer_t * timer_play= NULL;  //设置定时器


static lv_timer_t * second_timer=NULL; //设置1s定时器


typedef struct
{
	 void * image;  //图片
}Play_t;



static lv_obj_t * as_bg=NULL;        //背景对象
static lv_obj_t * battery_num=NULL;  //全局的电量对象
static lv_obj_t * heart_num=NULL;    //全局的心率对象
static lv_obj_t * step_num=NULL;     //全局的电量对象
static lv_obj_t * colo_num=NULL;     //全局的卡路里对象






//背景图片索引
Play_t play[]=
{
[0]			={.image=(void *)&as_bg1},
[1]		={.image=(void *)  &as_bg2},
[2]			={.image=(void *)&as_bg3},
[3]		={.image=(void *)  &as_bg4},
[4]			={.image=(void *)&as_bg5},
[5]		={.image=(void *)  &as_bg6},
[6]			={.image=(void *)&as_bg7},
[7]		={.image=(void *)  &as_bg8},	
};

//背景星期索引
Play_t Week[]={
[0]			={.image=(void *)&week_1},
[1]		={.image=(void *)  &week_2},
[2]			={.image=(void *)&week_3},
[3]		={.image=(void *)  &week_4},
[4]			={.image=(void *)&week_5},
[5]		={.image=(void *)  &week_6},
[6]			={.image=(void *)&week_7},
};
//时间索引
Play_t Time_[]={
[0]			={.image=(void *)&time_0},
[1]		={.image=(void *)  &time_1},
[2]			={.image=(void *)&time_2},
[3]		={.image=(void *)  &time_3},
[4]			={.image=(void *)&time_4},
[5]		={.image=(void *)  &time_5},
[6]			={.image=(void *)&time_6},
[7]			={.image=(void *)&time_7},
[8]			={.image=(void *)&time_8},
[9]			={.image=(void *)&time_9},
[10]			={.image=(void *)&time_colon},
};

static uint32_t angle=0,minute_angle=1500,hour_angle=0;


static void my_timer(lv_timer_t * timer)
{
//	printf("%s\r\n",__FUNCTION__);
	lv_img_set_angle(second_img,angle);
	
	
	if(angle>=3600)
	{
		angle=0;
		minute_angle+=60;
		lv_img_set_angle(minute_img,minute_angle);  
	}
	angle+=60;
}

static void my_second_timer(lv_timer_t * timer)
{
	    //lv_event_send(current_time,LV_EVENT_REFRESH,NULL);
		lv_label_set_text_fmt(current_time, "#ffffff %2d:%2d#",My_time.hour,My_time.minute);
		lv_label_set_text_fmt(time_obj, "#ffffff %02d:%02d:%02d#",My_time.hour,My_time.minute,My_time.second);
     My_time.second++;
	   if( My_time.second>=60)
		 {
		     My_time.second=0;
			    My_time.minute++;
			 if(My_time.minute>60)
			 {
					My_time.minute=0;
				  My_time.hour++;
						 if(My_time.hour>=24)
						 {
								My_time.hour=0;
						 }
			 } 
		 }

}


char play_count=0;
static void my_timer_play(lv_timer_t * timer)
{
    lv_img_set_src(as_bg, play[play_count].image);
	play_count++;
	if(play_count>=7)play_count=0;
}
static void my_event_cb(lv_event_t * event)
{
	
	printf("LV_EVENT_LONG_PRESSED\r\n");
	
	delete_watch_face_task();//暂停定时器
	watch_face_switch();     //表盘切换
}	



static void drag_event_handler(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);

    lv_indev_t * indev = lv_indev_get_act();
    if(indev == NULL)  return;

    lv_point_t vect;
    lv_indev_get_vect(indev, &vect);

    lv_coord_t x = lv_obj_get_x(obj) + vect.x;
    lv_coord_t y = lv_obj_get_y(obj) + vect.y;
    lv_obj_set_pos(obj, x, y);
}

void window_main_init()
{
	printf("%s  Current_Face=%d\r\n",__FUNCTION__,Current_Face);
	Watch_Option(Current_Face);    //表盘显示
}




//表盘切换
void Watch_Option(Watch_Face_t num)
{
    switch(num)
		{
			case	Watch_Face_1:	
				watch_face_1_init();    //太空人表盘 
	break;			
			case	Watch_Face_2:
				watch_face_2_init();   //指针表盘
	break;					
			case	Watch_Face_3:
				watch_face_3_init();
	break;					
			case	Watch_Face_4:	
				watch_face_4_init();
	break;					
			case	Watch_Face_5:
				watch_face_5_init();
	break;					
			case	Watch_Face_custom:
				watch_face_custom_init();
	break;	
			default :
     break;				
		
		}
}


void watch_face_1_init()
{
	  printf("%s\r\n",__FUNCTION__);
    //太空人表盘
		//lv_disp_set_bg_image(lv_obj_get_disp(window_manger[WINDOW_MAIN].window_obj),&bg);// 设置背景图片
    lv_obj_set_scrollbar_mode(window_manger[WINDOW_MAIN].window_obj, LV_SCROLLBAR_MODE_OFF);  //取消滚动条显示
		//背景动画
		as_bg= lv_img_create(window_manger[WINDOW_MAIN].window_obj);
		lv_img_set_src(as_bg, &as_bg1);
		lv_obj_set_pos(as_bg,100,180);
		//左上	
		lv_obj_t *	as_line= lv_img_create(window_manger[WINDOW_MAIN].window_obj);
		lv_img_set_src(as_line, &left_up);
		lv_obj_set_pos(as_line,180,0);
	  //上线
		lv_obj_t *	mid_l= lv_img_create(window_manger[WINDOW_MAIN].window_obj);
		lv_img_set_src(mid_l, &mid_line);
		lv_obj_set_pos(mid_l,5,100);
		//显示蓝牙图标
	  lv_obj_t *	bt= lv_img_create(window_manger[WINDOW_MAIN].window_obj);
    lv_img_set_src(bt, &bt_on);
		lv_obj_set_pos(bt,100,20);
		
		
		//显示电量图标
	  lv_obj_t *	bat_img= lv_img_create(window_manger[WINDOW_MAIN].window_obj);
    lv_img_set_src(bat_img, &bat);
		lv_obj_set_pos(bat_img,200,40);
		
		//显示电量数值
		
    //显示当前时间
		time_obj = lv_label_create(window_manger[WINDOW_MAIN].window_obj);
		lv_label_set_recolor(time_obj, true);                        /*Enable re-coloring by commands in the text*/
		lv_label_set_text_fmt(time_obj, "#ffffff %02d:%02d:%02d#",My_time.hour,My_time.minute,My_time.second);
    lv_obj_set_style_text_font(time_obj, &time_72, 0);
		lv_obj_align(time_obj, LV_ALIGN_TOP_MID,0,110);  //设置坐标组中显示
    second_timer=lv_timer_create(my_second_timer, 1000,NULL);	  
		timer=lv_timer_create(my_timer_play, 100,NULL);	 
		
		lv_obj_add_flag(window_manger[WINDOW_MAIN].window_obj,LV_OBJ_FLAG_CLICKABLE ); //增加可以长按的属性
		lv_obj_add_state(window_manger[WINDOW_MAIN].window_obj,LV_STATE_PRESSED);
		//lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_PRESSING,NULL);   //添加对象回调函数
    lv_obj_add_event_cb(window_manger[WINDOW_MAIN].window_obj/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_LONG_PRESSED,NULL);   //添加对象回调函数

		
}
void watch_face_2_init()
{

	#if 0
    lv_obj_t * img1 = lv_img_create(window_manger[WINDOW_MAIN].window_obj);
    lv_img_set_src(img1, &bg);
	  //lv_obj_get_disp(img1);
	  //lv_disp_set_bg_image(lv_obj_get_disp(img1),&bg);// 设置背景图片
	  //lv_obj_set_style_local_pattern_image(screen,LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,&img_bg);
	
	
	  hour_img = lv_img_create(img1);
    lv_img_set_src(hour_img, &hour);
		lv_obj_set_pos(hour_img,360/2-13,100);
	
		minute_img = lv_img_create(img1);
    lv_img_set_src(minute_img, &minute);
		lv_obj_set_pos(minute_img,360/2-10,60);

    second_img = lv_img_create(img1);
    lv_img_set_src(second_img, &second);
	 	lv_obj_set_pos(second_img,360/2-10,60);

		lv_img_set_pivot(second_img, 9, 110);
		lv_img_set_pivot(minute_img, 10, 110);
		lv_img_set_pivot(hour_img, 10, 67);
    timer=lv_timer_create(my_timer, 1000,NULL);	  
		
		
		lv_obj_add_flag(img1,LV_OBJ_FLAG_CLICKABLE ); //增加可以长按的属性
		lv_obj_add_state(img1,LV_STATE_PRESSED);
		
		//lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_PRESSING,NULL);   //添加对象回调函数
    lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_LONG_PRESSED,NULL);   //添加对象回调函数
		
		#endif
    
}




void watch_face_3_init()
{
	LV_IMG_DECLARE(time06_nackground);
	
	
	#if 1
	lv_obj_t * img1 = lv_img_create(window_manger[WINDOW_MAIN].window_obj);

    lv_img_set_src(img1, &time06_nackground);

	  //lv_obj_get_disp(img1);
	  //lv_disp_set_bg_image(lv_obj_get_disp(img1),&bg);// 设置背景图片
	  //lv_obj_set_style_local_pattern_image(screen,LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,&img_bg);
	
	lv_obj_t * img = lv_img_create(window_manger[WINDOW_MAIN].window_obj);
	lv_img_set_src(img, &conlon_2);
  lv_obj_set_pos(img,117,137);

//		LV_IMG_DECLARE(main2_min);
//		hour_img = lv_img_create(img1);
//		lv_img_set_src(hour_img, &main2_min);
//		lv_obj_set_pos(hour_img,0,0);
	
	  hour_img =  lv_img_create(window_manger[WINDOW_MAIN].window_obj);;
    lv_img_set_src(hour_img, &face_2_hour);
		lv_obj_set_pos(hour_img,240/2-11,123 -40);
	
		minute_img =  lv_img_create(window_manger[WINDOW_MAIN].window_obj);;
    lv_img_set_src(minute_img, &face_2_minute);
		lv_obj_set_pos(minute_img,240/2-11,67- 40);

    second_img =  lv_img_create(window_manger[WINDOW_MAIN].window_obj);;
    lv_img_set_src(second_img, &face_2_second);
	 	lv_obj_set_pos(second_img,240/2-2,60 -40);

		lv_img_set_pivot(second_img, 2, 119);
		lv_img_set_pivot(minute_img, 11, 110);
		lv_img_set_pivot(hour_img, 11, 62);
		
    timer=lv_timer_create(my_timer, 1000,NULL);	  
		
		lv_obj_add_flag(img1,LV_OBJ_FLAG_CLICKABLE ); //增加可以长按的属性
		lv_obj_add_state(img1,LV_STATE_PRESSED);
		
		//lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_PRESSING,NULL);   //添加对象回调函数
    lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_LONG_PRESSED,NULL);   //添加对象回调函数

	lv_img_set_angle(hour_img,  1800);
	lv_img_set_angle(minute_img,1500);
	lv_img_set_angle(second_img,0);

#else



LV_IMG_DECLARE(main_3_bg);
LV_IMG_DECLARE(main_3_hour);
LV_IMG_DECLARE(main_3_min);
LV_IMG_DECLARE(main_3_point);
LV_IMG_DECLARE(main_3_sec);
LV_IMG_DECLARE(watchface_3);








    lv_obj_t * img1 = lv_img_create(window_manger[WINDOW_MAIN].window_obj);
    lv_img_set_src(img1, &main_3_bg);
	  //lv_obj_get_disp(img1);
	  //lv_disp_set_bg_image(lv_obj_get_disp(img1),&bg);// 设置背景图片
	  //lv_obj_set_style_local_pattern_image(screen,LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,&img_bg);
	
	
	  hour_img = lv_img_create(img1);
    lv_img_set_src(hour_img, &hour);
		lv_obj_set_pos(hour_img,0,0);
	
		minute_img = lv_img_create(img1);
    lv_img_set_src(minute_img, &main_3_min);
		lv_obj_set_pos(minute_img,0,0);

    second_img = lv_img_create(img1);
    lv_img_set_src(second_img, &main_3_sec);
	 	lv_obj_set_pos(second_img,0,0);

		lv_img_set_pivot(second_img, 360/2, 360/2);
		lv_img_set_pivot(minute_img, 360/2, 360/2);
		lv_img_set_pivot(hour_img,   360/2, 360/2);
    timer=lv_timer_create(my_timer, 1000,NULL);	  
		
		
		lv_obj_add_flag(img1,LV_OBJ_FLAG_CLICKABLE ); //增加可以长按的属性
		lv_obj_add_state(img1,LV_STATE_PRESSED);
		
		//lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_PRESSING,NULL);   //添加对象回调函数
    lv_obj_add_event_cb(img1/*window_manger[WINDOW_MAIN].window_obj*/,my_event_cb,LV_EVENT_LONG_PRESSED,NULL);   //添加对象回调函数













#endif

}
void watch_face_4_init()
{



}
void watch_face_5_init()
{




}
void watch_face_custom_init()
{



}


void delete_watch_face_task()
{
   if(second_timer!= NULL)
	 {
		  lv_timer_del(second_timer);
		 
		 second_timer=NULL;
	 }
	  if(timer!= NULL)
		{
		  lv_timer_del(timer);
			 timer=NULL;
		}
}
